package com.astrob.adapters;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.astrob.nhcmap.R;

import java.util.ArrayList;

public class PlanFuncAdapter extends BaseAdapter {
	static class ViewHolder{
		ImageView img;
		TextView text;
		ImageButton extend;
	}
	public static class PlanFuncItem{
		public int imgId;
		public String text;
	}
	Context mContext = null;
	ArrayList<PlanFuncItem> mItems;
	public PlanFuncAdapter(Context context, ArrayList<PlanFuncItem> items){
		mContext = context;
		mItems = items;
	}
	@Override
	public int getCount() {
		return mItems.size();
	}

	@Override
	public Object getItem(int position) {
		return mItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	PlanFuncListener mListener = null;
	public void setListener(PlanFuncListener listener){
		mListener = listener;
	}
	public interface PlanFuncListener{
		public void onFunc(int pos);
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (null == convertView) {
			convertView = LinearLayout.inflate(mContext,
					R.layout.adapter_addroutepoint_func, null);

			holder = new ViewHolder();
			holder.img = (ImageView)convertView.findViewById(R.id.addroutepoint_img);
			holder.text = (TextView) convertView.findViewById(R.id.addroutepoint_text);
			holder.extend = (ImageButton)convertView.findViewById(R.id.addroutepoint_extend);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		PlanFuncItem item = mItems.get(position);
		holder.img.setBackgroundResource(item.imgId);
		holder.text.setText(item.text);
		final int pos = position;
		holder.extend.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(mListener != null){
					mListener.onFunc(pos);
				}
			}
		});
		holder.text.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(mListener != null){
					mListener.onFunc(pos);
				}				
			}
		});
		return convertView;
	}

}
